﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Util.Net.Http;
using Util.Date;

namespace Util.Net.Stat
{
    public class StatBaiduCookie
    {//
        public string BaiduKey { get; private set; }

        private static readonly string FirstVisitTimeKeyPrefix = "Hm_lvt_";
        private static readonly string LastVisitTimeKeyPrefix = "Hm_lpvt_";

        private DateTime FirstVisitTime { get; set; } // 第一次访问的时间
        private DateTime LastVisitTime { get; set; } // 最近一次访问的时间

        private CookieContainer Cookies { get; set; }
        private WebProxy Proxy { get; set; }

        private Cookie FirstVisitCookie { get; set; }
        private Cookie LastVisitCookie { get; set; }
        private Cookie ShareCookie { get; set; }

        private string VisitDomain { get; set; }
        private string ShareDomain { get; set; }

        private static readonly int VisitTimeTricksDivide = 1000 * 1000 * 10;
        private static readonly int ShareTimeTricksDivide = 1000 * 10;

        public StatBaiduCookie(string baiduKey, string visitDomain, CookieContainer cookies, WebProxy proxy = null)
        {
            this.BaiduKey = baiduKey;
            this.VisitDomain = visitDomain;
            this.Cookies = cookies;
            this.Proxy = proxy;
            this.LastVisitTime = this.FirstVisitTime = DateTime.Now;
            this.AddFirstVisitCookie();
            this.AddLastVisitCookie();
        }

        public void RefreshCurrentUri()
        {
            this.SetLastVisitCookie();
        }
        public void EnterTargetUri(Uri targetUri)
        {
            this.VisitBaiduForId(targetUri);
            this.SetLastVisitCookie();
        }

        private void SetLastVisitCookie()
        {
            this.LastVisitTime = DateTime.Now;
            this.LastVisitCookie.Value = (DateUtility.DateToTimestamp(this.LastVisitTime) / VisitTimeTricksDivide).ToString();
        }

        public void AddShareCookie(string shareDomain)
        {
            Cookie cookie = null;
            if (this.ShareCookie == null)
            {
                cookie = new Cookie();
                cookie.Name = "bdshare_firstime";
                cookie.Domain = shareDomain;
                this.Cookies.Add(cookie);
            }
            else
            {
                cookie = this.ShareCookie;
            }
            cookie.Value = (DateUtility.DateToTimestamp(DateTime.Now) / ShareTimeTricksDivide).ToString();
            cookie.Domain = shareDomain;
            cookie.Path = "/";
        }

        private void AddFirstVisitCookie()
        {
            Cookie cookie = new Cookie();

            cookie.Name = FirstVisitTimeKeyPrefix + this.BaiduKey;
            cookie.Value = (DateUtility.DateToTimestamp(this.FirstVisitTime) / VisitTimeTricksDivide).ToString();
            cookie.Domain = this.VisitDomain;
            cookie.Path = "/";

            this.Cookies.Add(cookie);
        }

        private void AddLastVisitCookie()
        {
            this.LastVisitCookie = new Cookie();

            this.LastVisitCookie.Name = LastVisitTimeKeyPrefix + this.BaiduKey;
            this.LastVisitCookie.Value = (DateUtility.DateToTimestamp(this.LastVisitTime) / VisitTimeTricksDivide).ToString();
            this.LastVisitCookie.Domain = this.VisitDomain;
            this.LastVisitCookie.Path = "/";

            this.Cookies.Add(this.LastVisitCookie);
        }

        private void VisitBaiduForId(Uri referUri)
        {
            HttpClient client = new HttpClient();
            client.CookieContainer = this.Cookies;
            client.Proxy = this.Proxy;
            client.Referer = referUri.AbsolutePath;
            try
            {
                client.GetResponseString("http://hm.baidu.com/h.js?" + this.BaiduKey);
                client.GetResponseString("http://pos.baidu.com/wh/o.htm");
            }
            catch (Exception) { }
        }
    }
}
